/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Board;
import java.io.File;
import parserosm.ParserOsm;
//import parserjson.*;
import java.util.Random;
import aimax.osm.data.impl.DefaultMap;
//import parserjson.*;
//import com.google.gson.*;
import aimax.osm.data.entities.MapNode;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.io.PrintWriter;
import java.lang.reflect.Field;
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.Enumeration;
import java.util.Hashtable;
//import java.util.Set;
/**
 *
 * @author cardozo
 */
public class MappingGenerator {

    public static String createMap() throws NoSuchFieldException, IllegalAccessException{
        File  file = new File("files\\ulm.osm");
        ParserOsm parseador = new ParserOsm(file);
        parseador.crearMapa();
        DefaultMap map = parseador.gethand();
        Random randomGenerator = new Random();
        int aux=0;
        String result = "{\"mapping\":[";
        MapNode mapaux = null;
        int i=0, j=0;
        int cantnodos = map.getNodeCount();
        int randnumber = randomGenerator.nextInt(cantnodos);

        Field f = map.getClass().getDeclaredField("nodes"); //NoSuchFieldException
        f.setAccessible(true);
        Hashtable<Long, MapNode> nodos = (Hashtable) f.get(map); //IllegalAccessException

        //Set<Long> claves = nodos.keySet();
        Object[] keys = nodos.keySet().toArray();
        int auxnode =0;

        int k=0;
        //while(k<100){
            for(i=0;i<4;i++){
                for(j=0;j<4;j++){
                    auxnode = randomGenerator.nextInt(randnumber);
                    result+="{\"boardPosition\":{\"row\":"+(i+1)+",\"col\":"+(j+1)+"},\"nodeId\":"+keys[auxnode].toString()+"},";
                }
            }
            k++;
        //}
        result = result.substring(0, result.length()-1);
        result+="]}";

        return result;
    }

    public static String createWorld(){
        String result="{\"problems\":[";
        int i=0, j=0, k=0, l=0;
        Random randomGenerator = new Random();
        
        //int randnumber = randomGenerator.nextInt(cantnodos);

        int orox=0, oroy=0;
        int[] pitsx = new int[3];
        int[] pitsy = new int[3];

        int wx = 0, wy=0;


        //while(l<100){
            for(k=0;k<1;k++){
                pitsx[0] = randomGenerator.nextInt(4)+1;
                pitsy[0] = randomGenerator.nextInt(4)+1;
                pitsx[1] = randomGenerator.nextInt(4)+1;
                pitsy[1] = randomGenerator.nextInt(4)+1;
                pitsx[2] = randomGenerator.nextInt(4)+1;
                pitsy[2] = randomGenerator.nextInt(4)+1;

                orox = randomGenerator.nextInt(4)+1;
                oroy = randomGenerator.nextInt(4)+1;

                wx = randomGenerator.nextInt(4)+1;
                wy = randomGenerator.nextInt(4)+1;
                //while(((orox==pitsx[0] && oroy == pitsy[0]) || (orox==pitsx[1] && oroy == pitsy[1]) || (orox==pitsx[2] && oroy == pitsy[2])) && (orox!=wx && oroy!=wy)){
                    pitsx[0] = randomGenerator.nextInt(4)+1;
                    pitsy[0] = randomGenerator.nextInt(4)+1;
                    pitsx[1] = randomGenerator.nextInt(4)+1;
                    pitsy[1] = randomGenerator.nextInt(4)+1;
                    pitsx[2] = randomGenerator.nextInt(4)+1;
                    pitsy[2] = randomGenerator.nextInt(4)+1;

                    orox = randomGenerator.nextInt(4)+1;
                    oroy = randomGenerator.nextInt(4)+1;

                    wx = randomGenerator.nextInt(4)+1;
                    wy = randomGenerator.nextInt(4)+1;
                //}
                result+="{\"wumpus\":{\"row\":"+ Integer.toString(randomGenerator.nextInt(4)+1)+",\"col\":"+Integer.toString(randomGenerator.nextInt(4)+1)+"},\"gold\":{\"row\":"+Integer.toString(randomGenerator.nextInt(4)+1)+",\"col\":"+Integer.toString(randomGenerator.nextInt(4)+1)+"},\"pits\":[{\"row\":"+Integer.toString(randomGenerator.nextInt(4)+1)+",\"col\":"+Integer.toString(randomGenerator.nextInt(4)+1)+"},{\"row\":"+Integer.toString(randomGenerator.nextInt(4)+1)+",\"col\":"+Integer.toString(randomGenerator.nextInt(4)+1)+"},{\"row\":"+Integer.toString(randomGenerator.nextInt(4)+1)+",\"col\":"+Integer.toString(randomGenerator.nextInt(4)+1)+"}]},";
            }
            result = result.substring(0, result.length()-1);
            result+="]}";
        //}


        return result;
    }
}
